package top.txl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.txl.Dto.DishDto;
import top.txl.Dto.SetmealDto;
import top.txl.entity.*;
import top.txl.mapper.*;
import top.txl.service.DishService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    @Transactional
    public void add(DishDto dishDto) {
        dishMapper.add(dishDto);
        Integer dishId = dishDto.getId();
        log.info("dish,{}", dishId);

        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            log.info("item,{}", item.getDishId());
            item.setDishId(dishId);
            item.setCreateTime(LocalDateTime.now());
            item.setUpdateTime(LocalDateTime.now());
            item.setCreateUser(dishDto.getCreateUser());
            item.setUpdateUser(dishDto.getUpdateUser());
            return item;
        }).collect(Collectors.toList());

        dishFlavorMapper.add(flavors);
    }


    @Override
    public PageBean lists(Integer page, Integer pageSize, Integer categoryId, String keyWords) {
        PageHelper.startPage(page, pageSize);
        List<Dish> lists = dishMapper.lists(keyWords, categoryId);
        Page p = (Page<Dish>) lists;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public DishDto detail(Integer id) {
        Dish dish = dishMapper.detail(id);
        log.info("dish,{}", dish);
        List<Flavors> flavors = dishFlavorMapper.detail(id);
        log.info("flavors,{}", flavors);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavorsList(flavors);
        log.info("dishDto,{}", dishDto);
        return dishDto;
    }

    @Override
    @Transactional
    public void update(DishDto dishDto) {
        dishMapper.updateDish(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();
        Integer dishId = dishDto.getId();

        List<Integer> ids = new ArrayList<>();
        ids.add(dishId);

        dishFlavorMapper.deleteFlavors(ids);
        // 先删除再添加

        if (flavors.size() > 0) {
            flavors = flavors.stream().map((item) -> {
                log.info("getDishId,{}", item.getDishId());
                item.setDishId(dishId);
                item.setCreateTime(LocalDateTime.now());
                item.setUpdateTime(LocalDateTime.now());
                item.setUpdateUser(dishDto.getUpdateUser());
                item.setCreateUser(dishDto.getUpdateUser());
                return item;
            }).collect(Collectors.toList());

            //  再添加
            dishFlavorMapper.add(flavors);
        }
    }

    @Override
    @Transactional
    public void delete(List<Integer> ids) {
        dishMapper.delete(ids);
        dishFlavorMapper.deleteFlavors(ids);
    }

    @Override
    public void updateStatus(DishDto dishDto) {
        dishMapper.updateStatus(dishDto);
    }

    @Override
    public List<DishDto> frontLists(Integer categoryId) {
        // 判断是套餐还是菜品 type 1-菜品  2-套餐
        List<Dish> dishs = dishMapper.frontLists(categoryId);
        List<DishDto> dtos = dishs.stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);

            Integer cId = item.getCategoryId();

            Category cateObj = categoryMapper.detail(cId);

            if (cateObj != null) {
                String name = cateObj.getName();
                dishDto.setCategoryName(name);
            }

            // 获取菜品id 根据菜品id 查询 规格数据
            List<DishFlavor> flavors = dishFlavorMapper.getFlavors(item.getId());
            dishDto.setFlavors(flavors);
            return dishDto;
        }).collect(Collectors.toList());

        return dtos;
    }

}
